<?php

namespace App\Http\Controllers\Admin;

use App\Models\AdminRole;
use App\Models\AdminUser;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

class AdminController extends Controller
{
    //
    public function index()
    {

        return view('admin.admin.index');
    }

    /**
     *  管理员列表数据post
     *
     */
    public function indexPost(Request $request)
    {

        $input = $request->all();
        $dd = (new AdminUser())->dataTable10(['*'], $input ,[
            'condition' => [
                [
                    'where',
                    ['where', 'name like %?%'],
                    ['orWhere', 'nickname like %?%'],
                ]
            ]
        ]);
        return response()->json($dd);
    }

    /**
     *  添加管理员
     *
     */
    public function addAdmin()
    {
        $role_list = AdminRole::all();

        return view('admin.admin.addAdmin',['role_list' => $role_list]);
    }

    public function addAdminPost(Request $request)
    {
        //dd($request->all());
        //对应表字段,如果表单字段对应数据字段可以省略
        $input =  [
            'name' => $request->input('name'),
            'nickname' => $request->input('nickname'),
            'avatar' => $request->input('file',''),
            'email' => $request->input('email',''),
            'password' => $request->input('password'),
            'introduction' => $request->input('introduction',''),
            'admin_role' => $request->input('admin_role','')        //数组形式
        ];
        if($request->has('id')){
            $res = (new AdminUser())->loadSave($input, $request->input('id'));
        }else{
            $info = AdminUser::where('name','=',$input['name'])->first();
            if(!empty($info)){
                return response()->json(['code'=>1,'msg'=>'账号已存在']);
            }
            $res = (new AdminUser())->loadSave($input);
        }
        return response()->json($res);
    }

    /**
     *  编辑管理员
     */
    public function editAdmin($id)
    {
        $role_list = AdminRole::all();
        $admin_info = AdminUser::find($id);
        $admin_info_role = $admin_info->roles()->get();

        return view('admin.admin.addAdmin',['admin' => $admin_info , 'admin_role' => $admin_info_role ,'role_list' => $role_list ] );
    }

    /**
     *  删除管理员
     */
    public function delAdmin(Request $request)
    {
        $id = $request->input('id');
        //判断是否批量删除
        if(is_numeric($id)){
            $re = AdminUser::find($id)->delete();
        }else{
            $ids = explode(',',$id);
            $re = DB::table('admin_users')->whereIn('id',$ids)->delete();
        }
        if($re){
            return response()->json(['code' => 1]);
        }else{
            return response()->json(['code' => 0]);
        }
    }

    /**
     *  图片上传
     */
    public function upload(Request $request)
    {
        if ($request->isMethod('post')) {
            $file = $request->file('file');
            // 文件是否上传成功
            if ($file->isValid()) {
                // 获取文件相关信息
                $originalName = $file->getClientOriginalName(); // 文件原名
                $ext = $file->getClientOriginalExtension();     // 扩展名
                $realPath = $file->getRealPath();   //临时文件的绝对路径
                $type = $file->getClientMimeType();     // image/jpeg
                // 上传文件
                $filename = date('Y-m-d-H-i-s') . '-' . uniqid() . '.' . $ext;
                // 使用我们新建的uploads本地存储空间（目录）
                $bool = Storage::disk('uploads')->put($filename, file_get_contents($realPath));
                if($bool){
                    $res = [
                        'code' => 1,
                        'status' => 'success',
                        'file' => $filename,
                        'path' => public_path('uploads').'/'.$filename,
                    ];
                }else{
                    $res = [
                        'code' => 0,
                        'status' => 'error',
                    ];
                }
                return response()->json($res);
            }

        }
    }

}
